package com.krain.meijusky.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.krain.corelibrary.util.ImageLorder;
import com.krain.corelibrary.widget.recyleview.BaseRecyclerViewAdapter;
import com.krain.meijusky.R;
import com.krain.meijusky.entity.ResourceEntity;

import java.util.List;
import java.util.Map;

/**
 * Class description
 *
 * @author yamin
 * @date 2015/11/19 下午 02:54
 */
public class SearchResultAdapter extends BaseRecyclerViewAdapter<SearchResultAdapter.ViewHolder> {

    private List<ResourceEntity> mList;
    private Context context;

    public SearchResultAdapter(List<ResourceEntity> mList, Context context) {
        this.mList = mList;
        this.context = context;
    }

    @Override
    public ViewHolder getViewHolder(View itemView) {
        return new ViewHolder(itemView);
    }

    @Override
    public View getItemView(int viewType, ViewGroup parent) {
        return LayoutInflater.from(context).inflate(R.layout.item_searchresult, parent, false);
    }



    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        ImageLorder.getInstance(context).loadImage(context, holder.item_sr_img, mList.get(position).getImg());
        StringBuffer buffer = new StringBuffer();
        for (Map.Entry entry : mList.get(position).getAttributes().entrySet()) {
            buffer.append(entry.getKey());
            buffer.append(entry.getValue());
            buffer.append("\n");
        }
        holder.item_sr_attr_tv.setText(buffer.toString());
        holder.item_sr_title_tv.setText(mList.get(position).getTitle());
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public  class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView item_sr_img;
        public TextView item_sr_title_tv, item_sr_attr_tv;

        public ViewHolder(View itemView) {
            super(itemView);
            item_sr_img = (ImageView) itemView.findViewById(R.id.item_sr_img);
            item_sr_title_tv = (TextView) itemView.findViewById(R.id.item_sr_title_tv);
            item_sr_attr_tv = (TextView) itemView.findViewById(R.id.item_sr_attr_tv);
        }
    }
}
